/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.tables;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author dpatchett
 */
@Entity
@Table(name = "badge")
@NamedQueries({
    @NamedQuery(name = "Badge.findAll",
            query = "SELECT b " +
                    "FROM Badge b"),
    @NamedQuery(name = "Badge.findByEmployeeId",
            query = "SELECT b " +
                    "FROM Badge b " +
                    "WHERE b.employeeId = :employeeId"),
    @NamedQuery(name = "Badge.findByBadgeId",
            query = "SELECT b " +
                    "FROM Badge b " +
                    "WHERE b.badgeId = :badgeId")})

public class Badge implements Serializable {

    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @Column(name = "badge_id")
    private Integer badgeId;

    @JoinColumn(name = "employee_id", referencedColumnName = "employee_id")
    @ManyToOne
    private Employee employeeId;

    public Badge() {
    }

    public Badge(Integer badgeId) {
        this.badgeId = badgeId;
    }

    public Integer getBadgeId() {
        return badgeId;
    }

    public void setBadgeId(Integer badgeId) {
        Integer oldBadgeId = this.badgeId;
        this.badgeId = badgeId;
        changeSupport.firePropertyChange("badgeId", oldBadgeId, badgeId);
    }

    public Employee getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Employee employeeId) {
        Employee oldEmployeeId = this.employeeId;
        this.employeeId = employeeId;
        changeSupport.firePropertyChange("employeeId", oldEmployeeId, employeeId);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (badgeId != null ? badgeId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Badge)) {
            return false;
        }
        Badge other = (Badge) object;
        if ((this.badgeId == null && other.badgeId != null) || (this.badgeId != null && !this.badgeId.equals(other.badgeId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "TimeRecording.tables.Badge[badgeId=" + badgeId + "]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

    public static HashMap<Integer, Employee> getBadgeMap(EntityManager entityManager) {

        Query query = entityManager.createNamedQuery("Badge.findAll");
        List<Badge> badges = query.getResultList();

        HashMap<Integer, Employee> badgeMap = new HashMap<Integer, Employee>();

        for (Badge badge : badges) {
            if (badge.getEmployeeId() != null) {
                badgeMap.put(badge.badgeId, badge.getEmployeeId());
            }
        }

        return badgeMap;
    }
}
